<?php


namespace app\home\controller;


use app\base\BaseController;
use think\facade\Db;

class SaleRefundDetail extends BaseController
{
    protected $title = '销售明细';
    protected $table = 'stock_order_detail';
    protected $sType=2;
    public function index()
    {
        $param = $this->request->param();
        if (request()->isAjax()) {
            $where[] = ['stod.status', '=', 2];
            $where[] = ['stod.type', '=', $this->sType];

            if (isset($this->param['orderNum']) && $this->param['orderNum']) {
                $where[] = ['sto.orderNum', 'like', '%'.$this->param['orderNum']."%"];
            }
            if (isset($this->param['pName']) && $this->param['pName']) {
                $where[] = ['py.name', 'like', '%'.$this->param['pName']."%"];
            }
            if (isset($this->param['specs']) && $this->param['specs']) {
                $where[] = ['md.specs', 'like', '%'.$this->param['specs']."%"];
            }
            if (isset($this->param['buyCode']) && $this->param['buyCode']) {
                $where[] = ['md.buyCode', 'like', '%'.$this->param['buyCode']."%"];
            }
            if (isset($this->param['brand']) && $this->param['brand']) {
                $where[] = ['md.brand', 'like', '%'.$this->param['brand']."%"];
            }
            if (isset($this->param['saleContractOrder']) && $this->param['saleContractOrder']) {
                $where[] = ['stod.saleContractOrder', 'like', '%'.$this->param['saleContractOrder']."%"];
            }
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $list=Db::name($this->table)->alias('stod')
                ->leftJoin('stock_order sto','sto.id=stod.orderId')
                ->leftJoin('material_dossier md','md.id=stod.mdId')
                ->leftJoin('purveyor py','py.id=sto.punterId')
                ->where($where)
                ->field('stod.*,py.name as pName,sto.orderNum as thOrderNum,md.name as mdName,md.buyCode,md.brand,md.specs,md.unit,md.bar_code')
                ->order('stod.id DESC')
                ->paginate($rows);
            return table_assign(0, '', $list);
        } else {
            return view();
        }
    }
}